package com.wrb.points.controller;

import com.wrb.points.exception.ServiceException;
import com.wrb.points.response.ServerResponse;
import com.wrb.points.service.PointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author wrb
 * @date 2019/11/25 11:40
 */
@RestController
@RequestMapping("/points")
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(tags = "积分服务")
public class PointsController {

	@Autowired
	private PointsService pointsService;

	@RequestMapping(value = "/get", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "获取用户积分")
	public ResponseEntity get(
			@ApiParam(required = true, value = "会员id") @RequestHeader(required = true) long customerId
	) {
		return ServerResponse.success(pointsService.getByCustomerId(customerId));
	}

	@RequestMapping(value = "/transPoints", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "增加用户积分")
	public ResponseEntity add(
			@ApiParam(required = true, value = "会员id") @RequestHeader(required = true) long customerId,
			@ApiParam(required = true, value = "更改积分总量") @RequestParam(required = true) int transPoints,
			@ApiParam(required = true, value = "更改积分类型（-1为减，1为加）") @RequestParam(required = true) int transType
	) throws ServiceException {
		pointsService.transPoints(customerId, transPoints, "手动加减积分", transType);
		return ServerResponse.success(pointsService.getByCustomerId(customerId));
	}



}
